/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.home2;

import java.io.StringReader;

import org.xml.sax.InputSource;

import android.app.Activity;
import android.isisufes.R;
import android.isisufes.ProfileManager.profile.General;
import android.isisufes.util.ServerCom;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SimpleProfileActivity extends Activity {
	private boolean isNewUser;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.simple_profile);
		
		// Verifica se é um novo usuário ou o perfil já existe.
		isNewUser = this.getIntent().getBooleanExtra("isnewuser", false);
		
		if (isNewUser) {
			// Esconde o botão de cancelar.
			this.findViewById(R.id.discard_button).setVisibility(View.GONE);
		} else {
			// Carrega o perfil anterior.
			this.loadPreviousData();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (isNewUser && keyCode == KeyEvent.KEYCODE_BACK) {
			Toast.makeText(this, "Please fill your profile.", Toast.LENGTH_SHORT).show();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public void acceptOnClick(View view) {
		String username = this.getIntent().getStringExtra("username");
		String password = this.getIntent().getStringExtra("password");

		String firstName = ((EditText) this.findViewById(R.id.first_name)).getText().toString().trim();
		String lastName = ((EditText) this.findViewById(R.id.last_name)).getText().toString().trim();
		
		if (firstName.length() <= 1 || lastName.length() <= 1) {
			Toast.makeText(this, "Please provide your first and last name.", Toast.LENGTH_SHORT).show();
			return;
		}
		
		General general = new General();
		general.firstName = firstName;
		general.lastName = lastName;
		// TODO Verificar se houve mudança em algum campo para poupar uma
		// atualização desnecessária com o servidor.
		ServerCom.updateUser(username, password, username, general.toXML());
		
		this.finish();
	}
	
	public void discardOnClick(View view) {
		this.finish();
	}
	
	private void loadPreviousData() {
		String username = this.getIntent().getStringExtra("username");
		String password = this.getIntent().getStringExtra("password");
		
		String xml = ServerCom.GetAction(username, password, username);
		
		xml = "<Items>" + xml + "</Items>";  // TODO Consertar essa lambança!
		
		General general = new General();
		general.fromXML(new InputSource(new StringReader(xml)));
		((EditText) this.findViewById(R.id.first_name)).setText(general.firstName);
		((EditText) this.findViewById(R.id.last_name)).setText(general.lastName);
	}
}
